package component.fy.com.common_module.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {
    /**
     * 倒计时时间戳转化为时分秒格式
     *
     * @param time
     * @return
     */
    public static String countDownTimerHour(long time) {
        //得到小时
        long hour = time / 1000 / 60 / 60;
        //减去小时
        long time1 = time - (hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time1 / 1000 / 60;
        //减去分
        long time2 = time1 - (minute * 60 * 1000);
        //得到秒
        long secs = time2 / 1000;
        return hour + "小时" + minute + "分" + secs + "秒";
    }

    /**
     * 倒计时时间戳转化为时分秒格式
     *
     * @param time
     * @return
     */
    public static String countDownTimerDay(long time) {
        //得到天
        long day = time / 1000 / 60 / 60 / 24;
        //减去天得到小时
        long time1 = time - (day * 24 * 60 * 60 * 1000);
        //得到小时
        long hour = time1 / 1000 / 60 / 60;
        //减去小时
        long time2 = time1 - (hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time2 / 1000 / 60;
        //减去分
        long time3 = time2 - (minute * 60 * 1000);
        //得到秒
        long secs = time3 / 1000;
        return day + "天" + hour + "小时" + minute + "分" + secs + "秒";
    }


    /**
     * 倒计时，仅显示小时，分钟，秒
     *
     * @param time
     * @return
     */
    public static String countDownTimerHourNoDay(long time) {
        //得到天
        long day = time / 1000 / 60 / 60 / 24;
        long dayTime = day * 24 * 60 * 60 * 1000;
        //减去天得到小时
        long time1 = time - dayTime;
        //得到小时
        long hour = time1 / 1000 / 60 / 60;
        long hourTime = hour * 60 * 60 * 1000;
        //减去小时
        long time2 = time1 - hourTime;
        //得到分钟数
        long minute = time2 / 1000 / 60;
        long minTime = minute * 60 * 1000;
        //减去分
        long time3 = time2 - minTime;
        //得到秒
        long secs = time3 / 1000;
//        Logg.e(TAG, "天数 = " + day + "，小时 = " + hour + "，分钟 = " + minute + "，秒 = " + secs);
//        Logg.e(TAG, "time1 = " + time1 + "，time2 = " + time2 + "，time3 = " + time3);
        return hour + ":" + minute + ":" + secs;
    }

    /**
     * 由字符串格式的时间戳获取到相应天数
     *
     * @param starTime
     * @param endTime
     * @return
     */
    public static String getDay(String starTime, String endTime) {
        long star = Long.parseLong(starTime);
        long end = Long.parseLong(endTime);
        long differ = end - star;
        String result = String.valueOf(differ / 60 / 60 / 24);
        return result;
    }

    /**
     * 得到天数
     *
     * @param time
     * @return
     */
    public static String getDay(long time) {
        String result = String.valueOf(time / 1000 / 60 / 60 / 24);
        return result;
    }

    /**
     * 倒计时得到单独的时分秒
     *
     * @param time
     */
    public static String[] countDownTimerTextview(long time) {
        String[] results = new String[6];
        //得到天
        long day = time / 1000 / 60 / 60 / 24;
        //减去天得到小时的毫秒
        long time1 = time - (day * 24 * 60 * 60 * 1000);
        //得到小时
        long hour = time1 / 1000 / 60 / 60;
        //减去小时得到分钟数的毫秒
        long time2 = time1 - (hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time2 / 1000 / 60;
        //减去分得到秒的毫秒
        long time3 = time2 - (minute * 60 * 1000);
        //得到秒
        long secs = time3 / 1000;
        String h = hour + "";
        String m = minute + "";
        String s = secs + "";
        if (h.length() == 2) {
            results[0] = h.charAt(0) + "";
            results[1] = h.charAt(1) + "";
        } else {
            results[0] = 0 + "";
            results[1] = h;
        }
        if (m.length() == 2) {
            results[2] = m.charAt(0) + "";
            results[3] = m.charAt(1) + "";
        } else {
            results[2] = 0 + "";
            results[3] = m;
        }
        if (s.length() == 2) {
            results[4] = s.charAt(0) + "";
            results[5] = s.charAt(1) + "";
        } else {
            results[4] = 0 + "";
            results[5] = s;
        }
        return results;
    }

    /**
     * 得到倒计时时间   0时0分0秒
     * @param time
     * @return
     */
    public static String[] countDownTimerNoDay(long time) {
        String[] results = new String[3];
        //得到小时
        long hour = time / 1000 / 60 / 60;
        //减去小时得到分钟数的毫秒
        long time1 = time - (hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time1 / 1000 / 60;
        //减去分得到秒的毫秒
        long time3 = time1 - (minute * 60 * 1000);
        //得到秒
        long secs = time3 / 1000;
        results[0] = hour + "";
        results[1] = minute + "";
        results[2] = secs + "";
        return results;
    }

    /**
     * 得到倒计时时间   0天0时0分0秒
     * @param time
     * @return
     */
    public static String[] countDownTimer(long time) {
        String[] results = new String[4];
        //得到天
        long day = time / 1000 / 60 / 60 / 24;
        //减去天得到小时的毫秒
        long time1 = time - (day * 24 * 60 * 60 * 1000);
        //得到小时
        long hour = time1 / 1000 / 60 / 60;
        //减去小时得到分钟数的毫秒
        long time2 = time1 - (hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time2 / 1000 / 60;
        //减去分得到秒的毫秒
        long time3 = time2 - (minute * 60 * 1000);
        //得到秒
        long secs = time3 / 1000;
        results[0] = day + "";
        results[1] = hour + "";
        results[2] = minute + "";
        results[3] = secs + "";
        return results;
    }

    /**
     * 将字符串转为时间戳
     *
     * @param user_time
     * @param type
     * @return
     */
    public static long getLongTime(String user_time, String type) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date d;
        long ltime = 0;
        try {
            d = sdf.parse(user_time);
            ltime = d.getTime();
            String str = String.valueOf(ltime);
//            re_time = str.substring(0, 10);//保留到秒
        } catch (ParseException e) {
            // TODO Auto-generated catch block e.printStackTrace();
        }
        return ltime;
    }

    /**
     * 获取活动结束的天数
     *
     * @param time 传入的结束时间
     * @return
     */
    public static String getTimeOfDay(String time) {
        long currentTime = System.currentTimeMillis();
        long endTime = Long.parseLong(time) * 1000;
        long diff = endTime - currentTime;
        if (diff >= 0) {
            long result = diff / 1000 / 60 / 60 / 24;
            return "剩余" + result + "天";
        } else {
            return "活动已结束";
        }
    }

    /**
     * 根据时间获取时间差，以xxx之前显示
     *
     * @param time
     * @return
     */
    public static String getTimeOfDiffType(String time) {
        long currentTime = System.currentTimeMillis();
        long starTime = Long.parseLong(time) * 1000;

        long diffTime = currentTime - starTime;
        long day = diffTime / 1000 / 60 / 60 / 24;
        long hour = diffTime / 1000 / 60 / 60;
        long min = diffTime / 1000 / 60;
        long secs = diffTime / 1000;
        if (day > 0) {
            return day + "天前";
        } else {
            if (hour > 0) {
                return hour + "小时前";
            } else {
                if (min > 0) {
                    return min + "分钟前";
                } else {
                    return secs + "秒前";
                }
            }
        }
    }

    /**
     * 把字符串转化为时间格式
     *
     * @param type      传入的时间显示格式
     * @param timestamp
     * @return
     */
    public static String getStrTime(String type, long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date date = new Date(timestamp);
        sdf.format(date);
        return sdf.format(date);
    }
}
